import torch
import torch.nn as nn
import torch.nn.functional as F

from torch.autograd import Variable
# from torch import optim
# from torch.utils import data

SOS_TOKEN = 0
EOS_TOKEN = 1

# http://blog.csdn.net/amds123/article/details/78129693


class Encoder(nn.Module):
    def __init__(self, input_size, hid_size, n_layers=1):
        super(Encoder, self).__init__()
        self.n_layers = n_layers
        self.hid_size = hid_size
        self.input_size = input_size

        self.embedding = nn.Embedding(input_size, hid_size)
        self.gru = nn.GRU(hid_size, hid_size)

    def forward(self, inp, hid):
        embedded = self.embedding(inp).view(-1, 1, -1)
        output = embedded
        for i in range(self.n_layers):
            output, hid = self.gru(output, hid)

        return output, hid

    def init_hidden(self):
        result = Variable(torch.zero(1, 1, self.hid_size))
        return result


class DecoderNoAttention(nn.Module):
    def __init__(self, input_size, hid_size, output_size, n_layers=1):
        super(DecoderNoAttention, self).__init__()
        self.hid_size = hid_size
        self.n_layers = n_layers
        self.embedding = nn.Embedding(input_size, hid_size)
        self.gru = nn.GRU(hid_size, hid_size)
        self.out = nn.Linear(hid_size, output_size)
        self.log_softmax = nn.LogSoftmax()

    def forward(self, inp, hid):
        output = self.embedding(inp).view(1, 1, -1)
        for i in range(self.n_layers):
            output = F.relu(output)
            output, hid = self.gru(output, hid)
        output = self.log_softmax(self.out(output[0]))

        return output, hid

    def init_hidden(self):
        return Variable(torch.zero(1, 1, self.hid_size))


class DecoderAttention(nn.Module):
    def __init__(self, hid_size, output_size, n_layers=1, dropout_p=0.1, max_len=40):
        super(DecoderAttention, self).__init__()
        self.hid_size = hid_size
        self.output_size = output_size
        self.n_layers = n_layers
        self.dropout_p = dropout_p
        self.max_len = max_len

        self.embedding = nn.Embedding(self.output_size, self.hid_size)
        self.attn = nn.Linear(self.hid_size * 2, self.max_len)
        self.attn_combine = nn.Linear(self.hid_size * 2, self.hid_size)
        self.dropout = nn.Dropout(self.dropout_p)
        self.gru = nn.GRU(self.hid_size, self.hid_size)
        self.out = nn.Linear(self.hid_size, self.output_size)

    def forward(self, inp, hid, encoded_output, encoded_outputs):
        embedded = self.embedding(inp).view(1, 1, -1)
        embedded = self.dropout(embedded)

        attn_weights = F.softmax(
            self.attn(torch.cat((embedded[0], hid[0]), 1)))

        attn_applied = torch.bmm(attn_weights.unsqueeze(0),
                                 encoded_outputs.unsqueeze(0))

        output = torch.cat((embedded[0], attn_applied[0]), 1)

        output = self.attn_combine(output).unsqueeze(0)

        for i in range(self.n_layers):
            output = F.relu(output)
            output, hid = self.gru(output, hid)

        output = F.log_softmax(self.out(output[0]))
        return output, hid, attn_weights

    def init_hidden(self):
        return Variable(torch.zeros(1, 1, self.hid_size))


def train(model, opt, data_loader, epoch):
    model.train()

    for batch_idx, (data, target) in enumerate(data_loader):
        data, target = data.cuda(), target.cuda()
        data, target = Variable(data), Variable(target)
        opt.zero_grad()
        output = model(data)
        loss = F.nll_loss(F.log_softmax(output), target)
        loss.backward()
        opt.step()
        if batch_idx % 100 == 0:
            print(batch_idx)
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch, batch_idx * len(data), len(data_loader.dataset),
                100. * batch_idx / len(data_loader), loss.data[0]
            ))


def gen_data_loader():
    pass
